package cn.edu.eight.food.controller;


import cn.edu.eight.food.model.OfNotice;
import cn.edu.eight.food.service.ofNoticeService;
import cn.edu.eight.food.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/notice")
public class ofNoticeController {
    @Autowired
    ofNoticeService ofNoticeService;

    @RequestMapping("/noticeList")
    @ResponseBody
    public Result noticeList(String titlename){
        List<OfNotice> noticeList = ofNoticeService.noticeList(titlename);
        Result<OfNotice> noticeResult = new Result<>();
        if (noticeList != null && noticeList.size() > 0 ){
            noticeResult.setCode("0");
            noticeResult.setMsg("请求成功");
            noticeResult.setCount(noticeList.size());
            noticeResult.setData(noticeList);
        }else {
            noticeResult.setCode("500");
            noticeResult.setMsg("没有数据！");
        }
        return noticeResult;
    }

    //添加公告
    @RequestMapping("/addNotice")
    @ResponseBody
    public Result addNotice(@RequestBody OfNotice ofNotice){
        Result result=new Result();
        try {
            ofNoticeService.insert(ofNotice);
            result.setCode("200");
        }catch (Exception exception){
            result.setCode("500");
        }
        return result;
    }

    //保存公告
    @RequestMapping("/saveNotice")
    @ResponseBody
    public Result saveNotice(@RequestBody OfNotice ofNotice){
        Result result=new Result();
        ofNoticeService.updateNotice(ofNotice);
        result.setCode("200");
        return result;
    }

    //删除公告
    @RequestMapping("/del")
    @ResponseBody
    public Result delNotice(@RequestBody String[] ids){
        Result result = new Result();
        for (int i=0;i<ids.length;i++){
            ofNoticeService.deleteNotice(ids[i]);
        }
        result.setCode("200");
        return result;
    }
}
